package com.bishe.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bishe.controller.utils.MD5;
import com.bishe.controller.utils.R;
import com.bishe.domain.Users;
import com.bishe.sevice.IUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
//@CrossOrigin
@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private IUsersService iUsersService;

    @GetMapping
    public R getAll(){
        return new R(true,iUsersService.list());

    }

    @PostMapping
    public R save(@RequestBody Users users) {
        /**将密码md5加密**/
        String password = users.getPassword();
        password  = MD5.md5Password(password);
        users.setPassword(password);
        /**将key也md5加密**/
        String kkey = users.getKkey();
        kkey  = MD5.md5Password(kkey);
        users.setKkey(kkey);
        return new R(iUsersService.save(users));
    }

    @PutMapping
    public R update(@RequestBody Users users){

        /**将密码md5加密**/

        String password = users.getPassword();
        password  = MD5.md5Password(password);
        users.setPassword(password);

        /**将key也md5加密**/

        String kkey = users.getKkey();
        kkey  = MD5.md5Password(kkey);
        users.setKkey(kkey);
        return new R(iUsersService.modify(users));
    }

    @DeleteMapping("{id}")
    public R delete(@PathVariable Integer id){
        return new R(iUsersService.delete(id));
    }

    @GetMapping("{id}")
    public R getById(@PathVariable Integer id){
        return new R(true,iUsersService.getById(id));
    }

   // @GetMapping("{currentPage}/{pageSize}")
    //public R getPage(@PathVariable int currentPage, @PathVariable int pageSize){
   //     IPage<Users> page = iUsersService.getPage(currentPage,pageSize);
    //    if(currentPage >page.getPages()){
     //       page = iUsersService.getPage((int)page.getPages(),pageSize);
    //    }
    //    return new R(true,page);
   // }

    @GetMapping("{currentPage}/{pageSize}")
    public R getPage(@PathVariable int currentPage, @PathVariable int pageSize){

        IPage<Users> page = iUsersService.getPage(currentPage,pageSize);
        if(currentPage >page.getPages()){
            page = iUsersService.getPage((int)page.getPages(),pageSize);
        }
        return new R(true,page);
    }

    @GetMapping("/cx/{uid}")
    public R GetBy(@PathVariable String uid){

        //List<Users> data = iUsersService.GetBy(uid);
        //Users users = data.get(0);
        //System.out.println(users);
        //String password = users.getPassword();
        //String a = password;
        //users.setPassword(a);
        //ArrayList<Users> users1 = new ArrayList<>();
        //users1.add(users);
        return new R(true, iUsersService.GetBy(uid));//users1);
    }

    /**登录用**/
    @GetMapping("/cx/{uid}/{password}")
    public List<String> GetBy2(@PathVariable String uid, @PathVariable String password){

        List<Users> data = iUsersService.GetBy2(uid,password);
        Users users = data.get(0);
        String password1 = users.getPassword();
        Integer judge = users.getJudge();
        String a = password1;
        String b = MD5.md5Password(password);
        List<String> users1 = new ArrayList<>();
        if(a.equals(b)){
            users1.add("true");
            users1.add(judge.toString());
            users1.add(users.getId().toString());
            users1.add(users.getUid());
        }
        else {
            users1.add("false");
        }
        return users1;
    }

    /**找回密码用**/
    @GetMapping("/key/{uid}/{kkey}")
    public List<String> GetBykkey(@PathVariable String uid,@PathVariable String kkey){

        List<Users> data = iUsersService.GetBykkey(uid,kkey);
        Users users = data.get(0);
        String kkey1 = users.getKkey();
        String a = kkey1;
        String b = MD5.md5Password(kkey);
        List<String> users1 = new ArrayList<>();
        if(a.equals(b)){
            users1.add("true");
            users1.add(users.getId().toString());
            users1.add(users.getUid());
            users1.add(users.getJudge().toString());
        }
        else {
            users1.add("false");
        }
        return users1;
    }
}
